package com.xiaotu.controller;

import com.xiaotu.pojo.Dept;
import com.xiaotu.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    //获取一些配置的信息,得到具体的微服务
    @Autowired
    private DiscoveryClient discoveryClient;
    @RequestMapping("/dept")
    public List<Dept> deptList(){
        System.out.println("springcloud");
        return deptService.selectAll();
    }
    @GetMapping("/dept/{id}")
    public Dept selectById(@PathVariable("id") Integer id){
        return deptService.selectById(id);
    }
    @PostMapping("/dept")
    public Boolean addDept(Dept dept){
        return deptService.insertDept(dept);
    }
    @GetMapping("/dept/discovery")
    //注册进来的微服务,,获取一些消息
    public Object discovery(){
        //获取微服务列表清单
        List<String> services = discoveryClient.getServices();
        System.out.println("discovery==>"+services);
        //得到具体的微服务信息   通过具体的微服务id   application.name
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDE-DEPT");
        for (ServiceInstance serviceInstance:instances) {
            System.out.println(serviceInstance.getHost()+"\t"
                    +serviceInstance.getPort()+"\t"
                    +serviceInstance.getUri()+"\t"
                    +serviceInstance.getServiceId());
        }
        return  this.discoveryClient;
    }
}
